package pages;

import javax.print.DocFlavor;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

/*
 * @类名：openApp
 * @作用：打开小程序
 * @作者：吴智海
 * @时间：2023.3.19
 */
public class openApp {
    private JLabel label;
    private JTextField userInput;
    private JPasswordField passwordText;
    private Container container;
    private imagePanel lowPage;
    private JFrame win;
    private Font titleFont = new Font("华文新魏", Font.BOLD, 37);
    private Font commonFont = new Font("华文新魏", Font.BOLD, 30);
    private Color commonColor = new Color(100, 180, 255);

    //        JPanel container = new JPanel();
    public openApp() {
        openAppWindows();
    }
    /*
     * @函数名：userLogin()
     * @作用：登录操作
     */
    private void userLogin() {
        label.setText("登录中.....");
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                String user = userInput.getText();
                String password = String.valueOf(passwordText.getPassword());
                System.out.println(user.equals("wuzhihai"));
                System.out.println(password.equals("wuzhihai"));
                if (user.equals("wuzhihai") && password.equals("wuzhihai")) {
                    label.setText("登录成功");
                    menuPage();
                } else {
                    label.setText("登录失败，请重新登录");
                }
            }
        }, 2000);
    }
    /*
     * @函数名：loginPage()
     * @作用：登录界面
     */
    private void loginPage(){
        lowPage.removeAll();
        //增加小组件层
        int start = 120;
        container = win.getContentPane();
        container.setLayout(null);

        label = creatLabel(0, start, 500, 70, "欢迎来到Omnipotent^-^", titleFont, new Color(0,255,80), SwingConstants.CENTER); //欢迎标签
        JLabel userLabel = creatLabel(70, start + 170, 120, 30, "用户名:", commonFont, commonColor);//用户名文本
        JLabel passWordLabel = creatLabel(70, start + 220, 120, 30, "密    码:", commonFont, commonColor);// 输入密码的文本
        JLabel loginButton = creatLabel(200, start + 300, 90, 60, "登录", commonFont, Color.red);// 创建登录按钮

        //输入用户名的输入
        userInput = new JTextField(20);
        userInput.setBounds(210, start + 170, 165, 37);

        /*
         *这个类似用于输入的文本域，但是输入的信息会以点号代替，用于包含密码的安全性
         */
        passwordText = new JPasswordField(20);
        passwordText.setBounds(210, start + 220, 165, 37);

        //增加事件
        loginButton.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userLogin();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {
                System.out.println("鼠标移入");
                loginButton.setForeground(Color.YELLOW);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                System.out.println("鼠标移出");
                loginButton.setForeground(Color.red);
            }
        });
        passwordText.addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyChar() == '\n') {
                    userLogin();
                }
            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });

        container.add(label);
        container.add(userLabel);
        container.add(userInput);
        container.add(passWordLabel);
        container.add(passwordText);
        container.add(loginButton);
    }
    /*
     * @函数名：menuPage()
     * @作用：菜单界面
     */
    private void menuPage(){
        System.out.println("进入主菜单");
        lowPage.removeAll();
        win.setContentPane(lowPage);
        int start = 300;

        label = creatLabel(0, 100, 500, 70, "Omnipotent", titleFont, new Color(0,255,80), SwingConstants.CENTER); //欢迎标签


        lowPage.add(label);
        lowPage.add(creatLabel(0,start,500,50,"第一项",commonFont,commonColor,Color.red,SwingConstants.CENTER));
        lowPage.add(creatLabel(0,start+80,500,50,"第二项",commonFont,commonColor,SwingConstants.CENTER));
        lowPage.add(creatLabel(0,start+160,500,50,"第三项",commonFont,commonColor,SwingConstants.CENTER));
    }
    /*
     * @函数名：openAppWindows()
     * @作用：打开程序窗口
     */
    private void openAppWindows() {
        //创建及设置窗口
        JFrame.setDefaultLookAndFeelDecorated(true);//美化窗体
        win = new JFrame("Omnipotent");//全能工具

        System.out.println(System.getProperty("user.dir") + "\\images\\lhicon.jpg");
        Toolkit tool = win.getToolkit();
        win.setBounds(200, 200, 500, 750);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setIconImage(tool.getImage(System.getProperty("user.dir") + "\\images\\lhicon.jpg"));
        win.setResizable(false);

        //增加背景图层
        lowPage = new imagePanel(win.getWidth(), win.getHeight(), System.getProperty("user.dir") + "\\images\\lh.jpg");
        win.setContentPane(lowPage);
        loginPage();
        win.setVisible(true);
    }
    private JLabel creatLabel(int x, int y, int wid, int hei, String text, Font font, Color color) {
        JLabel labels = new JLabel(text);
        labels.setFont(font);
        labels.setForeground(color);
        labels.setBounds(x, y, wid, hei);
        return labels;
    }
    private JLabel creatLabel(int x, int y, int wid, int hei, String text, Font font, Color color, int align) {
        JLabel labels = new JLabel(text);
        labels.setFont(font);
        labels.setForeground(color);
        labels.setBounds(x, y, wid, hei);
        labels.setHorizontalAlignment(align);
        return labels;
    }
    private JLabel creatLabel(int x, int y, int wid, int hei, String text, Font font, Color color,Color backcolor, int align) {
        JLabel labels = new JLabel(text);
        labels.setFont(font);
        labels.setForeground(color);
        labels.setBackground(backcolor);
        labels.setBounds(x, y, wid, hei);
        labels.setHorizontalAlignment(align);
        return labels;
    }
}
